<template>
  <div class="head-page">
    <el-container class="head-container">
      <el-header>
        <el-col :span="12" class="header-left">
          <img src="@/assets/logo.jpg" alt="logo" class="logo" />
          <span class="header-name">智慧眼科医疗平台</span>
        </el-col>
        <el-col :span="12" class="header-right">
          <img src="@/assets/OIP-C.jpg" alt="avatar" class="logo" />
          <span>{{ username }}</span>
          <el-button type="primary" @click="handleLogout">退出登录</el-button>
        </el-col>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu default-active="1" class="el-menu-vertical-demo">
            <el-menu-item index="1" @click="router.push('/user/home')"
              >首页</el-menu-item
            >
            <el-menu-item index="2" @click="router.push('/user/patientInfo')"
              >患者信息</el-menu-item
            >
            <el-menu-item index="3" @click="router.push('/user/diagnosisInfo')"
              >诊断信息</el-menu-item
            >
            <el-menu-item
              index="4"
              @click="router.push('/user/diseaseDiagnosis')"
              >病情诊断</el-menu-item
            >
            <el-menu-item index="5" @click="router.push('/user/ai-assistant')"
              >AI助手</el-menu-item
            >
            <el-menu-item index="6" @click="router.push('/user/info')"
              >用户信息</el-menu-item
            >
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted, nextTick } from "vue";
const instance = getCurrentInstance();
const proxy = instance?.proxy as any;
import { useRouter, Router } from "vue-router";
const router: Router = useRouter();
import { useUserStore } from "@/stores/userStore";
const userStore = useUserStore();

import { UserInfo } from "@/types/user";
import { RequestParams } from "@/types/request";

const userData: UserInfo = userStore.getUserData();

const username = ref<string>("默认用户名");
const userId = ref<number | null>(null);
const token = ref<string>("");

onMounted(() => {
  if (userData) {
    username.value = userData.username;
    userId.value = userData.userId;
    token.value = userData.token;
    router.push("/user/home");
  } else {
    proxy.message.error("未找到用户信息，请重新登录");
    router.push("/login");
  }
});

const handleLogout = async () => {
  await nextTick();
  try {
    let result: any = await proxy.request({
      method: "post",
      url: proxy.api.usersLogout,
      data: {} as RequestParams,
      headers: {
        authentication: token.value,
      },
    });
    if (!result) {
      return;
    }
    if (result.code === 1) {
      proxy.message.success("已退出登录");
      sessionStorage.removeItem("userData");
      router.push("/login");
    } else if (result.code === 401) {
      proxy.message.warning("退出登录失败");
    }
  } catch (error: any) {
    proxy.message.error(error.response?.data?.msg);
    sessionStorage.removeItem("userData");
    router.push("/login");
  }
};
</script>

<style scoped>
.head-page {
  background-color: #f0f4f8;
}
.head-container {
  margin-top: 0 !important;
  height: 100% !important;
}
.el-header {
  border-radius: 8px;
  background-color: #409eff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 auto;
  width: 100%;
}
.el-header .header-left {
  display: flex;
  align-items: center;
}
.el-header .logo {
  height: 40px;
  margin-right: 10px;
  border-radius: 100%;
}
.el-header .header-name {
  font-size: 24px;
  font-weight: bold;
}
.el-header .header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.el-header .header-right .el-avatar {
  margin-right: 10px;
}
.el-container {
  margin-top: 20px;
  height: calc(100vh - 100px);
}

.el-aside {
  background-color: #fff;
  border-right: 1px solid #ebeef5;
  border-radius: 10px;
}
.el-main {
  background-color: #f8f8f8;
  border-radius: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  margin-left: 20px;
  overflow: auto;
  padding: 0;
}
</style>
